package com.ruyuan.engine.router;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import java.util.Map;
import java.util.Set;

public class HBaseSearchService {
    Table table;
    String columnFamilyName;

    public HBaseSearchService(Connection connection,String tableName,String columnFamilyName) throws Exception {
        table = connection.getTable(TableName.valueOf(tableName));
        this.columnFamilyName = columnFamilyName;
    }

    public boolean matchBaseRuleCondition(Map<String,String> condition,String userId) throws Exception {
        Set<String> conditionNames = condition.keySet();
        Get get = new Get(userId.getBytes());
        for (String cName:conditionNames) {
            get.addColumn(columnFamilyName.getBytes(),cName.getBytes());
        }
        String value = "";
        Result result = table.get(get);
        for (String cName:conditionNames) {
            byte[] value1 = result.getValue(columnFamilyName.getBytes(), cName.getBytes());
            if (value1 != null) {
                value = new String(value1);
                if (!value.equals(condition.get(cName))) {
                    return false;
                }
            }else {
                return false;
            }
        }
        return true;
    }
}
